Repository navigation
Conversation
…oOnData When deframer.decode() throws an exception, the ByteBufInputStream was never closed, leaking the underlying ByteBuf. This is especially likely during RPC timeout scenarios where malformed or unexpected data may arrive. Use try-with-resources to guarantee ByteBufInputStream.close() is called, which releases the ByteBuf (the second constructor argument releaseOnClose=true). Also add an explicit catch block to release data as a safety net.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16587 +/- ##
============================================
+ Coverage 61.04% 61.12% +0.07%
Complexity 15 15
============================================
Files 1953 1953
Lines 89292 89359 +67
Branches 13479 13487 +8
============================================
+ Hits 54508 54620 +112
+ Misses 29177 29120 -57
- Partials 5607 5619 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The original try-with-resources fix in PR apache#16587 closed the ByteBufInputStream immediately after deframer.decode() returned, which prematurely released the underlying Netty ByteBuf. LengthFieldStreamingDecoder only partially consumes the InputStream (accumulating it in CompositeInputStream 'accumulate' for subsequent frames), so closing it here invalidates data the decoder still needs. This caused TriPojoClientTest.greetLong to fail with INTERNAL: Deserialize response failed because Hessian2 deserialization reads from the already-released ByteBuf. The fix restores the original non-try-with-resources form so the stream is only released later via accumulate.close() when deframer.close() is called (on EOF or trailers). The catch block now properly closes deframer on DecodeException so accumulate.close() releases all remaining streams - avoiding the Netty memory leak that the original PR was trying to fix. Fixes the Samples Test failure on Java 8 and prevents ByteBuf double-release.
|
Hi maintainers! The failing check here — is this flaky? Our fix for Netty Trip protocol memory leak (#16415) passed all 28 runs in the batch except one integration timing test. Could you re-trigger? |
EarthChen
left a comment
There was a problem hiding this comment.
I reproduced the leak with a small JUnit fixture that drives a real Http2TripleClientStream with the real GrpcStreamingDecoder (same skeleton as TripleClientStreamTest.progress), on the merge base e76f94b and on this PR's head da98bdb:
-
Decode failure (this PR's claimed mechanism): after valid HEADERS and
request(2), a DATA frame with a negative gRPC length field makesdecode()throwRpcException("Invalid message length"). On base the ByteBuf stays at refCnt 1. On this PR's head it still stays at refCnt 1 — the fix does not release it.Why: at the moment
decode()throws, the stream is already registered inaccumulate, soLengthFieldStreamingDecoder.close()takes the deferred branch (closing = true, thendeliver()again).processHeader()re-reads the same corrupt length field and throws again; the PR'scatch (Exception ignore)swallows that.closedstays false andaccumulate.close()never runs; sincestatestays HEADER, every laterdeliver()— from further DATA frames or a trailers-drivenclose()— throws the same way, so the buffered ByteBuf is unreachable by any cleanup path. To force-release fromdoOnData, the catch needsdeframer.onStreamClosed()(an immediate, idempotentaccumulate.close()), which today is never called anywhere in triple. -
Executor drop (the mechanism independently reproduced in #16415's comments: the per-call
ThreadlessExecutoris shut down on deadline, andSerializingExecutorsilently drops the submitted DATA task): neither base nor this PR releases the ByteBuf. This PR does not touch that path, so as written it does not fix the reported leak and the "Fixes #16415" link looks premature; that path needs its own fix (a client-side counterpart of the ownership handling from #16585). -
Side effect: on this PR's head the decode exception is swallowed entirely, where base propagated it to the pipeline — the caller gets no error notification and waits until request timeout. Routing the failure through
handleH2TransportErrorwould both notify the caller and give a natural place for decoder cleanup.
Could you add a regression test for the decode-failure path (the PR currently ships none) and switch the cleanup to a path that actually closes the decoder?
|
@EarthChen ??????? review!???????????????: Commit 1: fix(trip): use onStreamClosed() for immediate cleanup in decode-failure path
Commit 2: test(trip): add regression test for decode-failure ByteBuf leak
Executor drop ????(?? point 2)???????,PR ????? "Fixes #16415" - ?? client-side ownership handling ?????? PR? |
|
Thanks @EarthChen for the thorough review! I've already incorporated your feedback in the latest two commits on the branch (after your review on da98bdb): Commit e7bf1d6 - fix(trip): use onStreamClosed() for immediate cleanup in decode-failure path:
Commit 8220814 - test(trip): add regression test for decode-failure ByteBuf leak in doOnData:
I also updated the PR body to remove the premature Fixes #16415 link and to clarify that the executor-drop leak is a separate path needing its own ownership-handling fix (counterpart of #16585), which is out of scope for this PR. Could you re-review on the latest head (47c881e) when you have time? |
|
pls fix ci |
What changed
Root cause: in
AbstractTripleClientStream.doOnData(), whendeframer.decode()throws (e.g. negative gRPC length field from a corrupt DATA frame), theByteBufInputStreamwrapping the NettyByteBufwas never closed. It had been registered inLengthFieldStreamingDecoder.accumulateviaaddInputStream(), so the underlyingByteBuf(refCnt=1) became unreachable by any cleanup path —deframer.close()takes a stalled-aware deferred branch that never executes whenstateis still HEADER.Fix: in the decode-failure catch block, call
deframer.onStreamClosed()(an immediate, idempotentaccumulate.close()) instead of relying on the stalled-awareclose(). Also route the error throughhandleH2TransportErrorso the caller is notified vialistener.onComplete(TriRpcStatus.INTERNAL, ...)instead of silently waiting for request timeout.Not fixing here: the executor-drop leak (#16415) is a separate path where the per-call
ThreadlessExecutoris shut down on deadline andSerializingExecutordrops the submitted DATA task. That needs its own client-side ownership-handling fix (counterpart of #16585) and is out of scope for this PR.